/**
 * Bluetooth AutoAnswer Program
 * @author punan
 * @time 2013.02.20
 */



package com.huaqin.punan.buleautoanswer;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {
	public static final String TAG = "AutoAnswerMain";
	public static final int DELAY_SECONDS_LEVEL_ONE = 1;
	public static final int DELAY_SECONDS_LEVEL_TWO = 3;
	public static final int DELAY_SECONDS_LEVEL_THREE = 5;
	public static final String PREFERENCES_AUTO_ANSWER = "BluetoothAutoAnswer";
	
	private TextView tv_ser_state;
	private TextView tv_rg_tip;
	private CheckBox cb_autorun;
	private RadioGroup mRadioGroup;
	private RadioButton rb_delay_one;
	private RadioButton rb_delay_two;
	private RadioButton rb_delay_three;
	private Button mStartButton;
	
	private SharedPreferences mAutoAnswerPreference;
	private String autoAnswerServiceName = "com.huaqin.punan.buleautoanswer.BluetoothAutoAnswerService";
	private boolean serviceRunningFlag = false;
	private Intent intent;

	private int mDelaySeconds = 1;
	private boolean autoRunSerFlag = false;
	private boolean debugFlag = true;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if(debugFlag)
        	Log.i(TAG, "MainActivity OnCreate");
        
        mAutoAnswerPreference = getSharedPreferences(PREFERENCES_AUTO_ANSWER,Context.MODE_PRIVATE);
		final SharedPreferences.Editor editor = mAutoAnswerPreference.edit();
        
        tv_ser_state = (TextView)findViewById(R.id.tv_service_status);
        tv_rg_tip = (TextView)findViewById(R.id.tv_radiogroup_tip);
        cb_autorun = (CheckBox)findViewById(R.id.cb_service_autorun);
        mRadioGroup = (RadioGroup)findViewById(R.id.myradiogroup);
        rb_delay_one = (RadioButton)findViewById(R.id.radiobtn1);
        rb_delay_two = (RadioButton)findViewById(R.id.radiobtn2);
        rb_delay_three = (RadioButton)findViewById(R.id.radiobtn3);
        mStartButton = (Button)findViewById(R.id.btn_start_ser);
        
        UpdateButtonText();
        
		intent = new Intent();
		intent.setClass(MainActivity.this, BluetoothAutoAnswerService.class);
        

        mDelaySeconds = mAutoAnswerPreference.getInt("auto_delay_seconds", 1);
        autoRunSerFlag = mAutoAnswerPreference.getBoolean("auto_run_flag", false);

        if(autoRunSerFlag){
        	cb_autorun.setChecked(true);
        	mStartButton.setEnabled(false);
        	if(!serviceRunningFlag){
        		
        		MainActivity.this.startService(intent);
        		UpdateButtonText();
        	}
        }
        
        if(mDelaySeconds == DELAY_SECONDS_LEVEL_ONE){
        	
        	rb_delay_one.setChecked(true);
        }else if(mDelaySeconds == DELAY_SECONDS_LEVEL_TWO){
        	
        	rb_delay_two.setChecked(true);
        }else if(mDelaySeconds == DELAY_SECONDS_LEVEL_THREE){
        	
        	rb_delay_three.setChecked(true);
        }
        
        if(debugFlag)
        	Log.i(TAG, "serviceRunFlag="+serviceRunningFlag+
        			",autoRunSerFlag="+autoRunSerFlag+
        			",mDelaySeconds="+mDelaySeconds);
        
        //Set Button Enable or Disable
        cb_autorun.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				serviceRunningFlag = isServiceWork(autoAnswerServiceName);
				if(isChecked){
					if(!serviceRunningFlag){

						MainActivity.this.startService(intent);
						UpdateButtonText();
					}
				}

				editor.putBoolean("auto_run_flag", isChecked);
				editor.commit();
				mStartButton.setEnabled(!isChecked);
				if(debugFlag)
					Log.i(TAG, "CheckBox isChecked="+isChecked);
			}});
        
        //Set the Delay Seconds
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch(checkedId){
				case R.id.radiobtn1:
					mDelaySeconds = DELAY_SECONDS_LEVEL_ONE;
					break;
				case R.id.radiobtn2:
					mDelaySeconds = DELAY_SECONDS_LEVEL_TWO;
					break;
				case R.id.radiobtn3:
					mDelaySeconds = DELAY_SECONDS_LEVEL_THREE;
					break;
				default:
					mDelaySeconds = DELAY_SECONDS_LEVEL_ONE;
					break;
				}
				
				BluetoothAutoAnswerService.delaySeconds = mDelaySeconds;
				
				editor.putInt("auto_delay_seconds", mDelaySeconds);
				editor.commit();
				
				if(debugFlag)
					Log.i(TAG, "RadioButton mDelaySeconds="+mDelaySeconds);
			}});
        
        //Manual Control
        mStartButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(serviceRunningFlag){
					
					MainActivity.this.stopService(intent);
				}else{
					
					MainActivity.this.startService(intent);
				}
				UpdateButtonText();
				
				if(debugFlag)
					Log.i(TAG, "mStartButton serviceRunFlag="+serviceRunningFlag);
			}});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	public boolean isServiceWork(String mServiceName) {
		ActivityManager myManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
				.getRunningServices(30);
		for (int i = 0; i < runningService.size(); i++) {
			if (runningService.get(i).service.getClassName().toString()
					.equals(mServiceName)) {
				return true;
			}
		}
		return false;
	}
	
	private void UpdateButtonText(){
		serviceRunningFlag = isServiceWork(autoAnswerServiceName);
        //Set Button Text
        if(serviceRunningFlag){
        	mStartButton.setText(R.string.btn_stop_ser);
        	tv_ser_state.setText(R.string.tv_ser_running);
        }else{
        	mStartButton.setText(R.string.btn_start_ser);
        	tv_ser_state.setText(R.string.tv_ser_stopped);
        }
	}
	
}
